package com.senven.api.controller;

import com.senven.api.commom.ResponseCode;
import com.senven.api.commom.ResponsePage;
import com.senven.api.commom.ResponseResult;
import com.senven.api.exception.GlobalException;
import com.senven.api.pojo.Student;
import com.senven.api.service.StudentService;
import com.senven.api.utils.ReponseResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @author YJF
 *
 */
@RequestMapping("/api/student")
@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;


    @GetMapping("/queryStudentList")
    @ResponseBody
    public ResponsePage<Student> getStudent(@RequestParam(value = "current", defaultValue = "1") int current,
                                            @RequestParam(value = "size",defaultValue = "10")int size){
        return studentService.queryStudentList(current,size);
    }


//    @RequestMapping("/queryStudentList")
//    @ResponseBody
//    public List<Student> queryStudentList(){
//        return studentService.queryStudentList();
//    }


    /**
     * ID查询学生信息
     * 路径中获取参数
     * @param id
     * @return
     */
    @GetMapping(value = "/queryStudentListById")
    @ResponseBody
    public Student queryStudentListById(@RequestParam("id") String id){
        return studentService.queryStudentListById(id);
    }


    /**
     * 插入一个学生实体
     * @param student
     */
    @RequestMapping(value = "/InsertStudent",method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult insertStudent(@RequestBody Student student){
        int result;
        try {
            result = studentService.insertStudent(student);
        } catch (Exception e) {
            throw new GlobalException(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "数据库错误");
        }

        if (result == 0){
            return ReponseResultUtil.error(-1,"插入失败");
        }else{
            return ReponseResultUtil.success("插入成功");
        }
    }
    /**
     * 更新一个学生实体
     * @param student
     */
    @RequestMapping(value = "/UpdateStudent",method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult updateStudent(@RequestBody Student student){
        //System.out.println(Student.getName());
        int result = studentService.updateStudent(student);
        if (result == 0){
            return ReponseResultUtil.error(-1,"更新失败");
        }else{
            return ReponseResultUtil.success("更新成功");
        }
    }

    /**
     * 删除一个学生实体
     * @param id
     */
    @RequestMapping(value = "/DeleteStudent/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult deleteStudent(@PathVariable String id){
        //System.out.println(Student.getName());
        int result = studentService.deleteStudent(id);
        if (result == 0){
            return ReponseResultUtil.error(-1,"删除失败");
        }else{
            return ReponseResultUtil.success("删除成功");
        }
    }
}
